package org.nettyServer.scheduled;

import cn.hutool.core.date.DateUtil;
import org.nettyServer.connectionManage.ConnectionManage;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ManageConnectionTask {


    @Scheduled(cron = "0 * * * * ?")
    public void cleanNotRegisterConnection() {
        // 遍历未注册连接列表，判断是否超时，超时则关闭连接
        long current = DateUtil.current();
        ConnectionManage.getNotRegisterClientInfos()
                .forEach(notRegisterClientInfo -> {
                    if (current - notRegisterClientInfo.getConnectTime() > 60000) {
                        notRegisterClientInfo.getChannel().close();
                    }
                });
        // 遍历已注册连接列表，判断是否超时，超时则关闭连接
        ConnectionManage.getClientInfos()
                .forEach(clientInfo -> {
                    if (current - clientInfo.getLastActiveTime() > 60000) {
                        clientInfo.getChannel().close();
                    }
                });
    }



}
